package com.mdinfo.gw.door.service.impl

import com.mdinfo.base.common.bean.CommonResult
import com.mdinfo.base.common.bean.DataGrid
import com.mdinfo.base.common.bean.Ref
import com.mdinfo.base.utils.ListUtil
import com.mdinfo.base.utils.StringUtil
import com.mdinfo.base.utils.TimeUtil
import com.mdinfo.gw.door.bean.Door
import com.mdinfo.gw.door.bean.DoorAccess
import com.mdinfo.gw.door.bean.DoorCardTimeBind
import com.mdinfo.gw.door.bean.DoorOpenTime
import com.mdinfo.gw.door.mapper.DoorAccessMapper
import com.mdinfo.gw.door.mapper.DoorCardTimeBindMapper
import com.mdinfo.gw.door.mapper.DoorMapper
import com.mdinfo.gw.door.mapper.DoorOpenTimeMapper
import com.mdinfo.gw.door.param.DoorOpenTimeAddParam
import com.mdinfo.gw.door.param.DoorOpenTimeBindMultiCardParam
import com.mdinfo.gw.door.param.DoorOpenTimeListQueryParam
import com.mdinfo.gw.door.service.IDoorOpenTimeService
import com.mdinfo.gw.door.vo.DoorOpenTimeListQueryVo
import com.mdinfo.system.bean.SysDept
import com.mdinfo.system.mapper.SysDeptMapper
import org.apache.commons.lang3.time.FastDateFormat
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * Created by Administrator on 2018/2/6.
 */
@Service
@Transactional
class DoorOpenTimeServiceImpl(
        var doorOpenTimeMapper:DoorOpenTimeMapper,
        var accessMapper: DoorAccessMapper,
        var deptMapper: SysDeptMapper,
        var doorMapper: DoorMapper,
        var cardTimeBindMapper: DoorCardTimeBindMapper

) : IDoorOpenTimeService {

    /**给门添加开放时间*/
    override fun add(param: DoorOpenTimeAddParam): CommonResult<Void>{

        val maxOrder:DoorOpenTime? = doorOpenTimeMapper.getMaxOrder(param.doorId!!)
        if(maxOrder!=null && maxOrder.orderNum!!>=16){
            return Ref.error("每个门只能添加16个开放时间")
        }
        var num = 1
        if(maxOrder!=null){
            num = maxOrder.orderNum!! + 1
        }

        var openTime = DoorOpenTime()
        openTime.accessId = param.accessId
        openTime.createTime = Date()
        openTime.deptId = param.deptId
        openTime.doorId = param.doorId
        openTime.endDate = FastDateFormat.getInstance("yyyy-MM-dd").parse(param.endDate)
        openTime.endTime = param.endTime
        openTime.name = param.name
        openTime.opMode = param.opMode
        openTime.startTime = param.startTime
        openTime.weekday = StringUtil.listInt2String(param.weekday)
        openTime.doorOrderNum = param.doorOrderNum
        openTime.orderNum = num

        doorOpenTimeMapper.insert(openTime)
        return Ref.ok()
    }

    /**列表查询*/
    override fun listQuery(param: DoorOpenTimeListQueryParam)
            : CommonResult<DataGrid<DoorOpenTimeListQueryVo>>{

        var list = doorOpenTimeMapper.listQuery(param.start!!,param.num!!,param.name)
        var count = doorOpenTimeMapper.countListQuery(param.start!!,param.num!!,param.name)
        var vos = list.map {
            var vo = DoorOpenTimeListQueryVo()
            vo.accessId = it.accessId
            vo.createTime = TimeUtil.formater3(it.createTime)
            vo.deptId = it.deptId
            vo.doorId = it.doorId
            vo.endDate = TimeUtil.formater1(it.endDate)
            vo.name = it.name
            vo.endTime = it.endTime
            vo.opMode = it.opMode
            vo.openTimeId = it.id
            vo.orderNum = it.orderNum
            vo.startTime = it.startTime
            vo.weekday = it.weekday!!.replace("1","一")
                    .replace("2","二")
                    .replace("3","三")
                    .replace("4","四")
                    .replace("5","五")
                    .replace("6","六")
                    .replace("7","日")

            var access:DoorAccess? = accessMapper.selectById(it.accessId)
            vo.accessName = access?.name?:""
            var door:Door? = doorMapper.selectById(it.doorId)
            vo.doorName = door?.name?:""
            var dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            vo.opModeStr = when(it.opMode){
                1 -> "单卡识别"
                2 -> "卡加密码"
                3 -> "定时开关门"
                4 -> "双卡识别"
                5 -> "定时报警"
                6 -> "密码"
                7 -> "首卡开门"
                else -> ""
            }
            vo
        }
        var dataGrid = DataGrid<DoorOpenTimeListQueryVo>()
        dataGrid.total = count
        dataGrid.rows = vos
        return Ref.ok(dataGrid)
    }

    /**多张卡绑定到开放时间上*/
    override fun bindMultiCard(param: DoorOpenTimeBindMultiCardParam)
            : CommonResult<Void>{

        var binds = cardTimeBindMapper.listByTimeId(param.timeId!!)
        var cardIds = binds.map{
            it.cardId
        }

        val difference = ListUtil.difference(param.cardIdList, cardIds)
        for(cardId in difference){
            var bind = DoorCardTimeBind()
            bind.cardId = cardId
            bind.timeId = param.timeId
            bind.createTime = Date()
            cardTimeBindMapper.insert(bind)
        }
        return Ref.ok()
    }
}